package org.shock.ss.sms.action;

import java.util.List;
import java.util.Map;

import org.shock.ss.sms.dao.AutoTextDao;


public class AutoTextAction extends BaseAction{
	private String tablename;//要操作的表名
	
	/**
	 * 开始生成代码
	 * @return
	 * @throws Exception 
	 */
	public String make() throws Exception
	{
		tablename=null==tablename ? "" : tablename.trim().toLowerCase(); 
		String addPageString="";//添加页面的代码
		String listPageString="";//添加页面的代码
		String IserviceString="";//service接口的代码
		String serviceString="";//service类的代码
		String IDaoString="";//dao接口的代码
		String DaoString="";//dao类的代码
		String beanString="";//bean类的代码
		String viewPageString="";//查看页面的代码
		
		
		
		AutoTextDao dao=new AutoTextDao();
		//开始获得表结构
		List<Map<String, String>> tbInfoList=dao.getTableInfo(tablename);
		
		
		//打印表字段
		String ziduan="";
		for (int i = 0; i < tbInfoList.size(); i++) {
			ziduan+=((Map)tbInfoList.get(i)).get("columnname")+",";
		}
		System.out.println("ziduan--------------->"+ziduan);
		
		
		
		//开始获得相应的页面代码（String格式）
		addPageString=dao.getAddPage(tbInfoList);
		listPageString=dao.getListPage(tbInfoList);
		IserviceString=dao.getIServicePage(tablename);
		serviceString=dao.getServicePage(tablename);
		IDaoString=dao.getIDaoPage(tablename);
		DaoString=dao.getDaoPage(tablename);
		beanString=dao.getBeanPage(tbInfoList, tablename);
		viewPageString=dao.getViewPage(tbInfoList);
		
		
		//开始生成相应的文件
		String filename="";
		String basePath="e:\\自动生成代码\\"+tablename+"相应代码\\";
		//生成添加页面
		filename=basePath+tablename+"Form.jsp";
		dao.makeFile(filename, addPageString);
		//生成分页列表页面
		filename=basePath+tablename+"List.jsp";
		dao.makeFile(filename, listPageString);
		//生成service接口
		filename=basePath+"service\\"+tablename.toLowerCase()+"\\"+dao.toUpperHead(tablename)+"Service.java";
		dao.makeFile(filename, IserviceString);
		//生成service类
		filename=basePath+"service\\"+tablename.toLowerCase()+"\\impl\\"+dao.toUpperHead(tablename)+"ServiceImpl.java";
		dao.makeFile(filename, serviceString);
		//生成dao接口
		filename=basePath+"dao\\"+tablename.toLowerCase()+"\\"+dao.toUpperHead(tablename)+"Dao.java";
		dao.makeFile(filename, IDaoString);
		//生成dao类
		filename=basePath+"dao\\"+tablename.toLowerCase()+"\\impl\\"+dao.toUpperHead(tablename)+"DaoImpl.java";
		dao.makeFile(filename, DaoString);
		//生成bean类
		filename=basePath+"bean\\"+dao.toUpperHead(tablename)+".java";
		dao.makeFile(filename, beanString);
		//生成查看页面代码
		filename=basePath+""+tablename+"View.jsp";
		dao.makeFile(filename, viewPageString);
		
		return SUCCESS;
	}

	public String getTablename() {
		return tablename;
	}

	public void setTablename(String tablename) {
		this.tablename = tablename;
	}
}
